<template>
  <div class="login-page">
    <div class="login-card">
      <div class="brand">
        <h1>欢迎回来</h1>
        <p>请登录以继续</p>
      </div>

      <form class="login-form" @submit.prevent="handleSubmit">
        <div class="input-group">
          <label>用户名</label>
          <input type="text" v-model="loginForm.username" :class="{ error: errors.username }" placeholder="请输入用户名" @keyup.enter="handleSubmit" />
          <span class="error-message" v-if="errors.username">{{ errors.username }}</span>
        </div>

        <div class="input-group">
          <label>密码</label>
          <div class="password-input">
            <input
              :type="showPassword ? 'text' : 'password'"
              v-model="loginForm.password"
              :class="{ error: errors.password }"
              placeholder="请输入密码"
              @keyup.enter="handleSubmit"
            />
            <button type="button" class="toggle-password" @click="showPassword = !showPassword">
              {{ showPassword ? '👁️' : '👁️‍🗨️' }}
            </button>
          </div>
          <span class="error-message" v-if="errors.password">{{ errors.password }}</span>
        </div>

        <div class="options">
          <label class="remember">
            <input type="checkbox" v-model="loginForm.remember" />
            <span>记住我</span>
          </label>
          <a href="#" class="forgot">忘记密码？</a>
        </div>

        <button type="submit" class="submit-btn" :disabled="isLoading">
          {{ isLoading ? '登录中...' : '登录' }}
        </button>

        <p class="signup-link">还没有账号？<a href="#">立即注册</a></p>
      </form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { useUser } from '@/store';
import { useRouter } from 'vue-router';

const router = useRouter();
const user = useUser();
const showPassword = ref(false);
const isLoading = ref(false);

const loginForm = reactive({
  username: '',
  password: '',
  remember: false
});

const errors = reactive({
  username: '',
  password: ''
});

const validateForm = () => {
  let isValid = true;
  errors.username = '';
  errors.password = '';

  if (!loginForm.username) {
    errors.username = '请输入用户名';
    isValid = false;
  }
  if (!loginForm.password) {
    errors.password = '请输入密码';
    isValid = false;
  }
  return isValid;
};

const handleSubmit = async () => {
  if (!validateForm()) return;

  isLoading.value = true;
  try {
    await user.login(loginForm);
    router.push('/');
  } catch (error) {
  } finally {
    isLoading.value = false;
  }
};
</script>

<style lang="scss" scoped>
:root {
  --primary-color: #3b82f6;
  --error-color: #ef4444;
}

.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: #f0f9ff; // 更改背景色为浅蓝色
  padding: 20px;
  position: relative;
  overflow: hidden;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 24px;
  padding: 40px;
  box-shadow: 0 10px 30px rgba(59, 130, 246, 0.15);
  animation: cardFloat 0.6s ease-out;
}

@keyframes cardFloat {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.brand {
  text-align: center;
  margin-bottom: 40px;

  .logo {
    width: 60px;
    height: 60px;
    margin-bottom: 16px;
  }

  h1 {
    font-size: 28px;
    background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 12px;
    font-weight: 700;
  }

  p {
    color: #666;
    font-size: 16px;
  }
}

.login-form {
  .input-group {
    margin-bottom: 24px;
    position: relative;

    label {
      display: block;
      margin-bottom: 8px;
      color: #333;
      font-size: 14px;
      font-weight: 500;
      transition: color 0.3s;
    }

    input {
      width: 100%;
      padding: 16px 20px;
      border: 1px solid #e2e8f0;
      border-radius: 16px;
      font-size: 15px;
      transition: all 0.3s ease;
      background: #f8fafc;
      color: #334155;
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);

      &::placeholder {
        color: #94a3b8;
        font-weight: 300;
      }

      &:hover {
        background: #fff;
        border-color: #cbd5e1;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.04);
      }

      &:focus {
        outline: none;
        border-color: #3b82f6;
        background: #fff;
        box-shadow:
          0 0 0 3px rgba(59, 130, 246, 0.12),
          0 4px 12px rgba(0, 0, 0, 0.05);
      }

      &.error {
        border-color: #ef4444;
        background: #fff;
        box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.12);
      }
    }

    .error-message {
      color: var(--error-color);
      font-size: 12px;
      margin-top: 6px;
      display: flex;
      align-items: center;
      gap: 4px;

      &::before {
        content: '⚠️';
        font-size: 12px;
      }
    }
  }

  .password-input {
    position: relative;

    .toggle-password {
      position: absolute;
      right: 20px;
      top: 50%;
      transform: translateY(-50%);
      min-width: 36px;
      height: 36px;
      display: flex;
      align-items: center;
      justify-content: center;
      background: #f1f5f9;
      border: none;
      border-radius: 10px;
      cursor: pointer;
      padding: 0;
      font-size: 16px;
      color: #64748b;
      transition: all 0.3s ease;

      &:hover {
        background: #e2e8f0;
        color: #3b82f6;
      }
    }

    input {
      padding-right: 65px;
    }
  }
}

.options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;

  .remember {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;

    input[type='checkbox'] {
      accent-color: #3b82f6;
    }

    span {
      font-size: 14px;
      color: #555;
    }
  }

  .forgot {
    font-size: 14px;
    color: #3b82f6;
    font-weight: 500;
    text-decoration: none;

    &:hover {
      text-decoration: underline;
    }
  }
}

.submit-btn {
  width: 100%;
  padding: 12px;
  background: #3b82f6;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 500;
  letter-spacing: 0.5px;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 4px 12px rgba(59, 130, 246, 0.25);

  &:hover {
    background: #2563eb;
    transform: translateY(-1px);
    box-shadow: 0 6px 15px rgba(59, 130, 246, 0.35);
  }

  &:active {
    background: #1d4ed8;
    transform: translateY(0);
  }

  &:disabled {
    background: #94a3b8;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
  }
}

.signup-link {
  text-align: center;
  margin-top: 24px;
  font-size: 14px;
  color: #666;

  a {
    color: #3b82f6;
    text-decoration: none;
    font-weight: 600;

    &:hover {
      text-decoration: underline;
    }
  }
}

@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
    margin: 20px;
  }

  .brand h1 {
    font-size: 22px;
  }
}
</style>
